package com.example.demo.method.patter;

public abstract class AbstractBubbleSorter {

    private int operations = 0;
    protected int length = 0;

    protected int doSort(){
        operations = 0;
        if (length <= 1){
            return operations;
        }
        for (int nextToLast = length - 2;nextToLast >= 0;nextToLast++){
            for (int index = 0;index <= nextToLast;index++){
                if (outOfOrder(index)){
                    swap(index);
                }
                operations++;
            }
        }
        return operations;
    }

    protected abstract void swap(int index);

    protected abstract boolean outOfOrder(int index);
}
